package cn.cxyxj.study110;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * 原始配置
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("cxyxj")
                .password("123").roles("admin", "user")
                .and()
                .withUser("security")
                .password("123").roles("user");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()  //开启配置
                .antMatchers("/cxyxj/**").hasRole("admin")   //访问/cxyxj/**下的路径，必须具备admin身份
                .antMatchers("/security/**").hasRole("user") //访问/security/**下的路径，必须具备user身份
                .antMatchers("/permitAll").permitAll() // 访问/permitAll路径，不需要登录
                .antMatchers("/denyAll").denyAll()
                .anyRequest() //其他请求
                .authenticated()//验证   表示其他请求只需要登录就能访问
                .and()
                .formLogin(); // 开启表单登陆

        http.csrf().disable();
    }

}
